import { request, type CustomAxiosRequestConfig } from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 登录接口
export interface LoginParams {
  username: string
  password: string
  remember?: boolean
}

// 登录响应数据结构（在统一响应的data字段中）
export interface LoginData {
  access_token: string
  refresh_token?: string
  token_type: string
  expires_in: number
  user: {
    id: string
    username: string
    name: string
    email: string
    department?: string
    position?: string
    phone?: string
    avatar?: string
    status: string
    is_verified: boolean
    roles: string[]
    permissions: string[]
    created_at: string
    updated_at: string
    last_login?: string
    login_count?: number
  }
}

// 登录响应类型（统一响应格式）
export type LoginResponse = ApiResponse<LoginData>

// 用户登录
export const login = (data: LoginParams) => {
  return request.post<LoginResponse>('/auth/login', data, {
    showLoading: true,
    loadingText: '登录中...',
    showError: false  // 关闭自动错误提示，让登录页面自己处理
  } as CustomAxiosRequestConfig)
}

// 用户登出
export const logout = () => {
  return request.post('/auth/logout', {}, {
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig)
}

// 刷新token响应类型
export type RefreshTokenResponse = ApiResponse<LoginData>

// 刷新token
export const refreshToken = (refreshToken: string) => {
  return request.post<RefreshTokenResponse>('/auth/refresh-token', {
    refresh_token: refreshToken
  }, {
    showLoading: false,
    showError: false
  } as CustomAxiosRequestConfig)
}

// 用户信息响应类型
export type UserInfoResponse = ApiResponse<LoginData['user']>

// 获取用户信息
export const getUserInfo = () => {
  return request.get<UserInfoResponse>('/auth/me', {
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig)
}

// 修改密码
export interface ChangePasswordParams {
  old_password: string
  new_password: string
  confirm_password: string
}

// 通用成功响应类型
export type SuccessResponse = ApiResponse<{ message: string }>

export const changePassword = (data: ChangePasswordParams) => {
  return request.post<SuccessResponse>('/auth/change-password', data, {
    showLoading: true,
    loadingText: '修改中...',
    showError: true,
    showSuccess: true
  } as CustomAxiosRequestConfig)
}

// 忘记密码
export const forgotPassword = (email: string) => {
  return request.post<SuccessResponse>('/auth/forgot-password', { email }, {
    showLoading: true,
    loadingText: '发送中...',
    showError: true,
    showSuccess: true
  } as CustomAxiosRequestConfig)
}

// 重置密码
export interface ResetPasswordParams {
  token: string
  password: string
  confirm_password: string
}

export const resetPassword = (data: ResetPasswordParams) => {
  return request.post<SuccessResponse>('/auth/reset-password', data, {
    showLoading: true,
    loadingText: '重置中...',
    showError: true,
    showSuccess: true
  } as CustomAxiosRequestConfig)
}

// 获取用户列表
export interface GetUsersParams {
  page?: number
  size?: number
  search?: string
  status?: string
  role?: string
  department?: string
  is_active?: boolean
}

export const getUsers = (params?: GetUsersParams) => {
  return request.get('/users', {
    params,
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig)
}